package cn.gcjxsc.service.integral;

import cn.gcjxsc.consts.CheckStatus;
import cn.gcjxsc.data.query.salesman.ApplyInfoQuery;
import cn.gcjxsc.data.vo.salesman.ApplyInfoVo;
import cn.gcjxsc.entity.merchant.Merchant;
import cn.gcjxsc.entity.merchant.Store;
import cn.gcjxsc.entity.salesman.ApplyInfo;
import cn.gcjxsc.repository.salesaman.ApplyInfoRepository;
import cn.gcjxsc.repository.specs.SalesmanSpec;
import cn.gcjxsc.service.merchant.MerchantService;
import cn.gcjxsc.service.merchant.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pumbaa
 * @since 1.0
 */
@Service
public class ApplyInfoService {
    @Autowired
    private ApplyInfoRepository applyInfoRepository;
    @Autowired
    private StoreService storeService;
    @Autowired
    private MerchantService merchantService;

    /**
     * 查询申请列表
     *
     * @param filter   过滤条件
     * @param page     当前页
     * @param pageSize 每页最大记录数
     * @return
     */
    public Page<ApplyInfo> queryApplyInfo(ApplyInfoQuery filter, int page, int pageSize) {
        filter.format();
        PageRequest pageRequest = new PageRequest(page - 1, pageSize, new Sort(Sort.Direction.DESC, "created"));
        return applyInfoRepository.findAll(SalesmanSpec.buildFilter(filter), pageRequest);
    }

    /**
     * 转换数据
     *
     * @param list 申请列表
     * @return
     */
    public List<ApplyInfoVo> converList(List<ApplyInfo> list) {
        List<Integer> storeIds = new ArrayList<>();
        List<Integer> merchantIds = new ArrayList<>();
        for (ApplyInfo applyInfo : list) {
            storeIds.add(applyInfo.getStoreId());
        }
        List<Store> stores = storeService.findByIds(storeIds);
        Map<Integer, Store> storeMap = new HashMap<>();
        for (Store store : stores) {
            merchantIds.add(store.getMerchantId());
            storeMap.put(store.getId(), store);
        }
        List<Merchant> merchants = merchantService.findByIds(merchantIds);
        Map<Integer, Merchant> merchantMap = new HashMap<>();
        for (Merchant merchant : merchants) {
            merchantMap.put(merchant.getId(), merchant);
        }
        List<ApplyInfoVo> voList = new ArrayList<>();
        for (ApplyInfo applyInfo : list) {
            Store store = storeMap.get(applyInfo.getStoreId());
            Merchant merchant = merchantMap.get(store.getMerchantId());

            ApplyInfoVo vo = convert(applyInfo);
            vo.setStoreName(store.getName());
            vo.setMerchantName(merchant.getName());
            voList.add(vo);
        }
        return voList;
    }

    /**
     * 查询会员正在申请的店铺id集
     *
     * @param memberId 会员id
     * @return
     */
    public List<Integer> queryMemberApplyStoreIds(Integer memberId) {
        ApplyInfoQuery filter = new ApplyInfoQuery();
        filter.setMemberId(memberId);
        filter.setStatus(CheckStatus.WAIT.getStatus());
        List<ApplyInfo> list = applyInfoRepository.findAll(SalesmanSpec.buildFilter(filter));
        List<Integer> stores = new ArrayList<>();
        for (ApplyInfo applyInfo : list) {
            stores.add(applyInfo.getStoreId());
        }
        return stores;
    }

    /**
     * 查询会员总申请记录数
     * @param memberId 会员id
     * @return
     */
    public long queryMemberApplyInfoCount(Integer memberId){
        return applyInfoRepository.countByMemberId(memberId);
    }

    /**
     * 会员是否存在申请记录
     * @param memberId 会员id
     * @param storeId 店铺id
     * @return
     */
    public boolean isMemberAppling(Integer memberId, Integer storeId){
        long count = applyInfoRepository.countByMemberIdAndStatusAndStoreId(memberId, CheckStatus.WAIT, storeId);
        return count > 0;
    }

    public ApplyInfoVo convert(ApplyInfo info) {
        ApplyInfoVo vo = new ApplyInfoVo();
        BeanUtils.copyProperties(info, vo);
        vo.setStatus(info.getStatus().getStatus());
        return vo;
    }
}
